package com.sec.etech.employeesync.handler;

import com.sec.etech.employeesync.entity.OrgEmployeeZSchool;
import com.sec.etech.employeesync.mapper.OrgEmployeeZSchoolMapper;
import com.sec.etech.employeesync.util.ConfigHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SchoolHandler implements ConfigHandler {

    @Resource
    private OrgEmployeeZSchoolMapper schoolMapper;

    @Override
    public Long createConfig(String... params) {
        String name = params[0];
//        String tag = params[1];
//        Integer tagInt = null;
//        if (StringUtils.isNotBlank(tag)) {
//            try {
//                tagInt = Integer.parseInt(tag);
//            } catch (NumberFormatException e) {
//                log.error("学校配置高校标签格式错误:tag={}", tag);
//            }
//        }

        OrgEmployeeZSchool school = new OrgEmployeeZSchool();
        school.setName(name);
//        school.setTag(tagInt);
        school.setActive(1);
        schoolMapper.insert(school);
        log.info("新增学校配置:name={}", name);

        return school.getSchoolId();
    }

    @Override
    public List<ConfigEntry> getAllConfigs() {
        return schoolMapper.selectList(null).parallelStream()
                .map(item -> {
                    ConfigEntry entry = new ConfigEntry();
                    entry.setId(item.getSchoolId());
                    entry.setParams(new String[]{item.getName()});
                    return entry;
                })
                .collect(Collectors.toList());
    }
}
